package BookManage;


import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class operation {
    private static String url="jdbc:mysql://127.0.0.1:3306/BookManage?characterEncoding=utf8&useSSL=false";
    private static String user="zhang";
    private static String password="xijie9401";
    private static DataSource dataSource=new MysqlDataSource();
    static{
        ((MysqlDataSource)dataSource).setURL(url);
        ((MysqlDataSource)dataSource).setUser(user);
        ((MysqlDataSource)dataSource).setPassword(password);
    }
    public static Connection My_Get_Connection() throws SQLException {
        return dataSource.getConnection();
    }
    public static void My_close(Connection connection, Statement statement, ResultSet resultSet){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static Timestamp getTimestamp(String dateString){
        try {
            java.util.Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateString);
            return new java.sql.Timestamp(date.getTime());
        } catch (ParseException e) {
            throw new RuntimeException("日期格式化错误："+dateString, e);
        }
    }
}
